package models

import (
	"gorm.io/gorm"
	"hoop_srv/basic/config"
	"time"
)

type Order struct {
	Id          uint           `gorm:"column:id;type:bigint(20) UNSIGNED;primaryKey;not null;" json:"id"`
	OrderSn     string         `gorm:"column:order_sn;type:varchar(255);comment:订单编号;not null;" json:"order_sn"`                                                       // 订单编号
	Title       string         `gorm:"column:title;type:varchar(30);comment:课程标题;not null;" json:"title"`                                                              // 课程标题
	TeacherId   uint           `gorm:"column:teacher_id;type:bigint(20);comment:名师ID;not null;" json:"teacher_id"`                                                       // 名师ID
	StartTime   time.Time      `gorm:"column:start_time;type:datetime(3);comment:开始时间;not null;" json:"start_time"`                                                    // 开始时间
	EndTime     time.Time      `gorm:"column:end_time;type:datetime(3);comment:结束时间;not null;" json:"end_time"`                                                        // 结束时间
	OrderStatus uint           `gorm:"column:order_status;type:tinyint(1) UNSIGNED;comment:订单状态：1:待支付，2:已支付，3:支付取消;not null;default:1;" json:"order_status"` // 订单状态：1:待支付，2:已支付，3:支付取消
	CreatedAt   time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt   time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt   gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (o *Order) TableName() string {
	return "order"
}
func (o *Order) CreateOrder() error { //创建订单
	return config.DB.Create(&o).Error
}
func (o *Order) UpdateStatus() error { //支付成功修改订单状态
	return config.DB.Model(&Order{}).Where("id = ?", o.Id).Update("order_status", o.OrderStatus).Error
}
